package com.wanmait.movie.controller;

import com.wanmait.movie.entity.DTO.GenSeatMapDTO;
import com.wanmait.movie.entity.DTO.GenSeatMapDTOTwo;
import com.wanmait.movie.entity.Lobby;
import com.wanmait.movie.entity.request.manage.lobby.AddLobbyRequest;
import com.wanmait.movie.entity.request.manage.lobby.UpLobbyRequest;
import com.wanmait.movie.entity.request.manage.lobby.UpLobbySeatRequest;
import com.wanmait.movie.service.LobbyService;
import com.wanmait.movie.service.SeatService;
import com.wanmait.movie.util.AjaxResult;
import com.wanmait.movie.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * ClassName: ManageLobbyController
 * Package: com.wanmait.movie.controller
 * Description:
 * 影厅管理
 * @Author Fengxd
 * @Create 2024/1/2 8:51
 * @Version 1.0
 */
@Validated
@RestController
@RequestMapping("manage/lobby")
public class ManageLobbyController {
    @Resource
    LobbyService lobbyService;

    @Resource
    SeatService seatService;

    /**
     * 获取影厅信息
     * @return 影厅信息
     */
    @GetMapping("lobbyInfo")
    public AjaxResult getLobbyInfo() {
        return AjaxResult.success("success",lobbyService.getLobbyInfo());
    }

    /**
     * 添加影厅信息
     * @param lobbyRequest 影厅信息
     * @param request request
     * @return 添加结果
     */
    @PostMapping("addLobby")
    public AjaxResult addLobby(@Validated @RequestBody AddLobbyRequest lobbyRequest, HttpServletRequest request) {
        Integer lobbyId = lobbyService.addLobby(lobbyRequest,request);
        if (lobbyId == null) {
            return AjaxResult.error("添加失败");
        }
        GenSeatMapDTOTwo genSeatMapDTO = new GenSeatMapDTOTwo();
        genSeatMapDTO.setCol(lobbyRequest.getCol());
        genSeatMapDTO.setRow(lobbyRequest.getRowNum());
        genSeatMapDTO.setRequest(request);
        genSeatMapDTO.setLobbyId(lobbyId);
        if (!seatService.genSeatMap(genSeatMapDTO)){
            return AjaxResult.error("座位创建失败");
        }
        return AjaxResult.success("添加成功");
    }

    /**
     * 更新影厅信息
     * @param lobbyRequest 影厅信息
     * @param request request
     * @return 更新结果
     */
    @PostMapping("upLobby")
    public AjaxResult upLobby(@Validated @RequestBody UpLobbyRequest lobbyRequest, HttpServletRequest request) {
        Lobby newLobby = new Lobby();
        BeanUtils.copyProperties(lobbyRequest,newLobby);
        Integer currentAdminId = Integer.parseInt(JWTUtils.getAudience(request.getHeader("token")));
        newLobby.setUpdateAdminId(currentAdminId);
        newLobby.setUpdateTime(new Date());
        if (!lobbyService.updateById(newLobby)){
            return AjaxResult.error("更新失败");
        }
        return AjaxResult.success("更新成功");
    }

    @PostMapping("upLobbySeat")
    public AjaxResult upLobbySeat(@Validated @RequestBody UpLobbySeatRequest upLobbySeatRequest, HttpServletRequest request) {
        if (!lobbyService.upLobbySeat(upLobbySeatRequest,request)) {
            return AjaxResult.error("更新失败");
        }
        return AjaxResult.success("更新成功");
    }

    /**
     * 删除影厅
     * @param id 影厅id
     * @param request request
     * @return 删除结果
     */

    @GetMapping("delete")
    public AjaxResult delete(@NotNull(message = "id不能为空") Integer id,HttpServletRequest request) {
        Integer currentAdminId = Integer.parseInt(JWTUtils.getAudience(request.getHeader("token")));
        Lobby lobby = new Lobby();
        lobby.setId(id);
        lobby.setUpdateAdminId(currentAdminId);
        lobbyService.updateById(lobby);
        if (!lobbyService.removeById(id)) {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }
}
